package domain;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class sales {
	private String name;
	private int buynum;
	
	public List<sales> list () throws ClassNotFoundException, SQLException {
		Class.forName("com.mysql.jdbc.Driver");
		List<sales> res = new ArrayList<sales>();
		Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/bookstore", "root", "");
		String strSQL = "select orderitem.buynum, products.name from orderitem inner join products on orderitem.product_id=products.id order by orderitem.buynum desc;";
		PreparedStatement pst = conn.prepareStatement(strSQL);
		ResultSet rs = pst.executeQuery();
		
		while (rs.next()) {
			sales s = new sales();
			s.setName(rs.getString(2));
			s.setBuynum(rs.getInt(1));
			res.add(s);
		}
		
		rs.close();
		pst.close();
		conn.close();
		
		return res;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public int getBuynum() {
		return buynum;
	}

	public void setBuynum(int buynum) {
		this.buynum = buynum;
	}
}
